﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class PostGraduateEducation
    {
        public void ImportList(List<Classes.PostGraduateEducation> PostGraduateEducationList)
        {
            var mdb = new MedregDBDataContext();

            var sSpec = new Classes.SertificateSpeciality();
            var Spec = new SertificateSpeciality();
            Spec.ImportList(sSpec.getSertificateSpecialityList(PostGraduateEducationList));

            var sEdIst = new Classes.EducationInstitution();
            var EdInst = new EducationInstitution();
            EdInst.ImportList(sEdIst.getEducationInstitutionList(PostGraduateEducationList));

            var sAD = new Classes.AcademicDegree();
            var AD = new AcademicDegree();
            AD.ImportList(sAD.getAcademicDegreeList(PostGraduateEducationList));
            
            var sPGET = new Classes.PostGraduationEducationType();
            var PGET = new PostGraduationEducationType();
            PGET.ImportList(sPGET.getPostGraduationEducationTypeList(PostGraduateEducationList));
            
            foreach (Classes.PostGraduateEducation postGraduateEducation in PostGraduateEducationList)
            {
                var postGraduateEducation1 = new PostGraduateEducation
                {
                    
                    BaseOrg = postGraduateEducation.BaseOrg.ID,
                    Type = postGraduateEducation.Type.ID,
                    DateBegin = postGraduateEducation.DateBegin,
                    DateEnd = postGraduateEducation.DateEnd,
                    DateDocum = postGraduateEducation.DateDocum,
                    Degree = postGraduateEducation.Degree != null ? postGraduateEducation.Degree.ID: (int?) null,
                    DiplomaSerie = postGraduateEducation.DiplomaSerie,
                    DiplomaNumber = postGraduateEducation.DiplomaNumber,
                    PostGraduationSpeciality = postGraduateEducation.PostGraduationSpeciality.ID,
                    Employee = postGraduateEducation.Employee.ID
                };

                IEnumerable<PostGraduateEducation> query = (from cr in mdb.PostGraduateEducation
                                                           where cr.Employee == postGraduateEducation.Employee.ID
                                                           select cr
                                                   ).ToList();
               
                mdb.PostGraduateEducation.DeleteAllOnSubmit(query);
                mdb.PostGraduateEducation.InsertOnSubmit(postGraduateEducation1);
            }
            
            mdb.SubmitChanges();
            
        }
    }
}
